package cn.project.lemontree.controller;

import cn.project.lemontree.aop.SystemControllerLog;
import cn.project.lemontree.bean.Album;
import cn.project.lemontree.bean.Child;
import cn.project.lemontree.bean.Parent;
import cn.project.lemontree.bean.User;
import cn.project.lemontree.service.AlbumService;
import cn.project.lemontree.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/album")
@Api(tags = "相册管理模块")
@CrossOrigin  //跨域访问
public class AlbumController {
    @Resource
    AlbumService albumService;

    @ApiOperation("分页查询相册信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="theme",value="主题",required=false,dataTypeClass = String.class),
            @ApiImplicitParam(name="parentId",value="家长序列号",required=false,dataTypeClass = Integer.class)
    })
    @GetMapping("/listpage")
    public CommonResult<Map<String ,Object>> listpage(int page, int size, String theme,Integer parentId){
        Map<String ,Object> map = new HashMap<>();
        map.put("data",albumService.listpage(page,size,theme,parentId));
        map.put("count",albumService.count(theme,parentId));
        map.put("page",page);
        return CommonResult.success(map);
    }

    @SystemControllerLog(description = "删除相册")
    @ApiOperation("删除相册信息")
    @GetMapping("/remove")
    public CommonResult remove(@RequestParam("album_id") int album_id){
        return CommonResult.success(albumService.removeById(album_id));
    }

    @SystemControllerLog(description = "上传相册")
    @ApiOperation("上传相册")
    @PostMapping("/save")
    public CommonResult save(@RequestParam("file") MultipartFile file, Integer parentId,Integer childId,String theme,String ddesc){
        Album album = new Album(theme,ddesc,new Child(childId),new Parent(parentId));
        return CommonResult.success(albumService.save(file,album));
    }
}
